﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace TdxConnector
{
    [ServiceContract(Namespace = "http://finlib.com/2013/10")]
    public interface ITdxService
    {
        [OperationContract]
        int SendOrder(int category, string zqdm, double price, int quantity);

        [OperationContract]
        bool CancelOrder(int nth);

        [OperationContract]
        Asset GetAsset();

        [OperationContract]
        Position[] GetPositions();

        [OperationContract]
        Order[] GetOrders(bool excludeCancelled = true);
    }

    [DataContract]
    public class Asset
    {
        /// <summary>
        /// 总资产
        /// </summary>
        [DataMember]
        public double Total { get; set; }
        /// <summary>
        /// 持仓市值
        /// </summary>
        [DataMember]
        public double MarketValue { get; set; }
        /// <summary>
        /// 多少资金可用购买
        /// </summary>
        [DataMember]
        public double Available { get; set; }
        /// <summary>
        /// 盈亏
        /// </summary>
        [DataMember]
        public double PnL { get; set; }
    }

    [DataContract]
    public class Position
    {
        [DataMember]
        public string InstrumentName { get; set; }
        [DataMember]
        public string InstrumentCode { get; set; }
        [DataMember]
        public int Quantity { get; set; }
        [DataMember]
        public int QuantityCanBeSold { get; set; }
        [DataMember]
        public int QuantityBoughtToday { get; set; }
        [DataMember]
        public int QuantitySoldToday { get; set; }
        [DataMember]
        public double Price { get; set; }
        [DataMember]
        public double MarketValue { get; set; }
        [DataMember]
        public double MarketPrice { get; set; }
        [DataMember]
        public double PnL { get; set; }
        [DataMember]
        public double PnlPct { get; set; }
    }

    [DataContract]
    public class Order
    {
        [DataMember]
        public string InstrumentName { get; set; }
        [DataMember]
        public string InstrumentCode { get; set; }
        [DataMember]
        public int Quantity { get; set; }
        [DataMember]
        public double Price { get; set; }
        [DataMember]
        public DateTime OrderTime { get; set; }
        [DataMember]
        public int Sequence { get; set; }
        [DataMember]
        public OrderType Type { get; set; }
        [DataMember]
        public OrderState State { get; set; }
        [DataMember]
        public double ExecutedPrice { get; set; }
        [DataMember]
        public int ExecutedQuantity { get; set; }
        [DataMember]
        public DateTime ExecutedTime { get; set; }
    }
    [Flags]
    public enum OrderState
    {
        None,
        Cancelled = 0x01,
        Buying = 0x02,
        Selling = 0x04,
        ExecutedBuy = 0x08,
        ExecutedSell = 0x10,

        Executed = ExecutedBuy | ExecutedSell,
        Buy = ExecutedBuy | Buying,
        Sell = ExecutedSell | Selling,
        Executing = Buying | Selling
    }

    public enum OrderType
    {
        None,
        Limit
    }
}
